package com.xiaoshangkou.hellopra.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.bean.RoleWithStatus;
import com.xiaoshangkou.hellopra.mapper.AdminMapper;
import com.xiaoshangkou.hellopra.mapper.RoleMapper;
import com.xiaoshangkou.hellopra.pojo.Admin;
import com.xiaoshangkou.hellopra.pojo.Permission;
import com.xiaoshangkou.hellopra.pojo.Role;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 *
 * 关于数据库中的管理员服务层
 *
 */
@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private BCryptPasswordEncoder encoder;//加密器

    //分页查询管理员
    public Page<Admin> findPage(int page,int size){
        Page selectPage=adminMapper.selectPage(new Page(page,size),null);
        return selectPage;
    }

    //新增管理员
    public void add(Admin admin){
        admin.setPassword(encoder.encode(admin.getPassword()));//加密
        adminMapper.insert(admin);
    }

    //启用/禁用管理员
    public void updateStatus(Integer aid){
        Admin admin = adminMapper.selectById(aid);
        admin.setStatus(!admin.isStatus());//取反
        adminMapper.updateById(admin);//更新数据
    }

    //根据aid查询管理员
    public Admin findById(Integer aid){
        return adminMapper.selectById(aid);
    }

    //修改管理员
    public void update(Admin admin){
        //旧密码
        String oldPassword = adminMapper.selectById(admin.getAid()).getPassword();
        //新密码
        String newPassword = admin.getPassword();
        //如果新密码不等于旧密码，对新密码进行加密
        if (!oldPassword.equals(newPassword)){
            admin.setPassword(encoder.encode(admin.getPassword()));
        }
        adminMapper.updateById(admin);
    }

    //查找所有角色，以及用户拥有的角色
    public List<RoleWithStatus> findRole(Integer aid){
        List<Role> roles = roleMapper.selectList(null);//查询所有角色
        List<Integer> roleIdByAdmin = roleMapper.findRoleIdByAdmin(aid);//用户拥有的角色
        List<RoleWithStatus> roleWithStatusList=new ArrayList<>();//创建带有状态的角色集合
        //查询用户拥有的角色对象并添加要返回的集合中
        for (Role role : roles) {
            RoleWithStatus roleWithStatus=new RoleWithStatus();
            //将role属性的值复制给roleWithStatus属性里面
            BeanUtils.copyProperties(role,roleWithStatus);
            if (roleIdByAdmin.contains(role.getRid())){//当角色循环到用户拥有的角色时
                roleWithStatus.setRoleHas(true);
            }else{
                roleWithStatus.setRoleHas(false);
            }
            roleWithStatusList.add(roleWithStatus);
        }

        return roleWithStatusList;

    }

    //给用户重新赋予角色
    public void updateRole(Integer[] ids,Integer aid){
        //删除用户拥有的所有角色
        adminMapper.deleteAdminAllRoles(aid);
        //重新添加角色
        for (Integer rid : ids) {
            adminMapper.addAdminRole(aid,rid);
        }
    }

    //查看详情
    public Admin findDesc(Integer aid){
        return adminMapper.findDesc(aid);
    }

    //根据名字查询管理员
    public Admin findByAdminName(String username){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return adminMapper.selectOne(wrapper);
    }

    //根据名字查询权限
    public List<Permission> findAllPermission(String username){
        return adminMapper.findAllPermission(username);
    }


}
